<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/sessionTools.js"></script>
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">博客主页</a>
        <a href="personal_center.html">个人中心</a>
        <a href="blog_add.html">写博客</a>
        <a href="blog_draft.html">草稿箱</a>
        <a href="Javascript:exit()">注销</a>
        
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left" style="display: none;">
            <div class="card">
                <!-- src="img/avatar.png" -->
                <img  class="avtar" alt="" id="image">
                <h3>小可爱</h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right" style="margin: 0 auto" id="myArticleList">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            
        </div>
    </div>

    <script>
        // 加载当前用户所有文章
        function getMyAllArticle() {
            jQuery.ajax({
                url:'/article/getAllArticle',
                type:"post",
                data:{},
                success:function(res){
                    if(res.code == 200 && res.data.length > 0) {
                        // 前端拿到的是一个文章集合
                        var artHtml="";

                        for(var i = 0; i < res.data.length; i++) {
                            // 拼接 div 以及内容
                            var artitem = res.data[i];
                            artHtml += '<div class="blog">';
                            artHtml += '<div class="title">'+ artitem.title +'</div>';
                            artHtml += '<div class="date">'+ artitem.createtime +'</div>';     
                            artHtml += '<div class="desc">';
                            artHtml += artitem.content;
                            artHtml += '</div>';
                            artHtml += '<a href="blog_content.html?id='+ artitem.id +'" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;'
                            artHtml += '<a href="blog_edit.html?id='+ artitem.id +'" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;'
                            artHtml += '<a href="Javascript:del('+ artitem.id +')" class="detail">删除 &gt;&gt;</a>';
                            artHtml += '</div>';
                        }
                        jQuery("#myArticleList").html(artHtml);// 将拼接内容设置回页面中
                    }else {
                        jQuery("#myArticleList").html("<h1 style='display: flex;justify-content: center;align-items: center;text-align: center;color: rgba(0, 0, 0, 0.5);'>里面空空如也</h1> ");
                    }
                }
            });
        }

        getMyAllArticle();

        function del(id) {
            if(confirm("是否确认删除?")) {
                jQuery.ajax({
                    url:'/article/del',
                    type:'post',
                    data:{'id':id},
                    success:function(res) {
                        if(res.code == 200 && res.data == 1) {
                            alert("删除成功!");
                            location.href = location.href;
                        }else {
                            alert("操作失败 :" + res.msg + ", 请稍后重试!");
                        }
                    }
                });
            }
        }

        

    </script>
</body>

</html>